<template>
  <div class="app-container">
    <transition name="el-zoom-in-top">
      <el-form v-show="searchShow" :inline="true" label-width="80px">
        <el-form-item label="账号名称">
          <el-input v-model="listQuery.title" size="small" />
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="handleSearchClick">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="handleSearchCleanClick">清空</el-button>
        </el-form-item>
      </el-form>
    </transition>

    <!-- 工具栏 -->
    <div class="tools-container" style="margin-bottom:45px; margin-top:20px">
      <div v-if="checkAdd" class="buttons">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAddClick">新建</el-button>
        <!-- </div>
        <div class="tags">-->
        <el-button
          size="mini"
          icon="el-icon-search"
          circle
          style="float:right"
          @click="handleSearchShowChange"
        />
        <el-button
          size="mini"
          icon="el-icon-refresh"
          circle
          style="float:right"
          @click="handleRefreshClick"
        />
        <el-button
          size="mini"
          icon="el-icon-menu"
          circle
          style="float:right"
          @click="handleHeaderClick"
        />
      </div>
    </div>

    <!-- 表头 -->
    <el-dialog
      :visible.sync="headerShow"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="表头"
    >
      <el-checkbox
        v-model="checkAll"
        :indeterminate="isIndeterminate"
        @change="handleCheckAllChange"
      >全选</el-checkbox>
      <div style="margin: 15px 0;" />
      <el-checkbox-group v-model="header">
        <el-checkbox
          v-for="(value, key) in columns"
          :key="key"
          :label="key"
          :prop="key"
        >{{ value.label }}</el-checkbox>
      </el-checkbox-group>
    </el-dialog>

    <!-- 表格 -->
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      class="table-container"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      @sort-change="handleSortChange"
    >
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column
        v-for="(value, key) in columns"
        v-if="value.show"
        :key="key"
        :label="value.label"
        :prop="key"
        sortable
        align="center"
      />
      <el-table-column align="center" label="所属影院" prop="filmName" />
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="checkEdit"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEditClick(scope.row)"
          >编辑</el-button>
          <el-button
            v-if="checkDelete"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDeleteClick(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 组件 -->
    <el-pagination
      :current-page.sync="listQuery.page"
      :page-size="listQuery.size"
      :total="total"
      :page-sizes="[5,10,20,30,50]"
      class="pagination-container"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 表单 -->
    <el-dialog
      :title="isCreate ? '新建记录' : '编辑记录'"
      :visible.sync="formShow"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="form" :model="row" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="电影封面">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="http://localhost:2333/api/v1/server/upload/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="uploadFilesSucceed"
            :file-list="fileList"
            :auto-upload="false"
          >
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button
              style="margin-left: 10px;"
              size="small"
              type="success"
              @click="submitUpload"
            >上传图片</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item v-if="!isCreate" label="主题封面" prop="haspromotionTag">
          <img :src=" row.haspromotionTag" style="height: 50px; width: 50px">
        </el-form-item>
        <el-form-item label="电影名称" prop="movieName">
          <el-input v-model="row.movieName" />
        </el-form-item>
        <el-form-item label="主演" prop="star">
          <el-input v-model="row.star" />
        </el-form-item>
        <el-form-item label="评分" prop="sc">
          <el-input v-model="row.sc" />
        </el-form-item>
        <el-form-item label="时长" prop="wish">
          <el-input v-model="row.wish" />
        </el-form-item>
        <el-form-item label="上映时间" prop="showst">
          <el-input v-model="row.showst" />
        </el-form-item>
        <el-form-item label="已上映场数" prop="showInfo">
          <el-input v-model="row.showInfo" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="row.price" />
        </el-form-item>
        <el-form-item label="是否3D" prop="version">
          <el-radio v-model="row.version" :label="tversion">是</el-radio>
          <el-radio v-model="row.version" :label="fversion">否</el-radio>
        </el-form-item>
        <el-form-item label="是否全球发布" prop="globalReleased">
          <el-radio v-model="row.globalReleased" :label="tglobalReleased">是</el-radio>
          <el-radio v-model="row.globalReleased" :label="fglobalReleased">否</el-radio>
        </el-form-item>
        <el-form-item label="剧情简介" prop="preShow">
          <el-input v-model="row.preShow" type="textarea" />
        </el-form-item>
        <el-form-item label="所属城市">
          <el-select v-model="row.cityId" placeholder="请选择" @change="changeSeleCity(row.cityId)">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属影院">
          <el-select v-model="row.filmId" placeholder="请选择">
            <el-option
              v-for="item in optionsfil"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleBeforeClose">取 消</el-button>
        <el-button v-if="isCreate" type="primary" @click="handleCreateClick">创 建</el-button>
        <el-button v-else type="primary" @click="handleUpdateClick">更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { page, columns, update, add, del } from '@/api/nowplay'
import { getcitylist } from '@/api/city'
import { getfilmlist } from '@/api/film'
import { toLowerCase } from '@/utils/toCase'
export default {
  name: 'Nowmoviing',
  data() {
    return {
      rules: {
      },
      tversion: 0,
      fversion: 1,
      tglobalReleased: 0,
      fglobalReleased: 1,
      fileList: [],
      list: null,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10,
        order: 'id',
        title: ''
      },
      total: 0,
      searchShow: false,
      formShow: false,
      isCreate: true, // true创建, false:更新
      headerShow: false,
      isIndeterminate: true,
      checkAll: true,
      columns: [],
      row: {
        movieName: '',
        star: '',
        sc: '',
        wish: '',
        showst: '',
        showInfo: '',
        price: '',
        cityId: '',
        filmId: '',
        version: '',
        globalReleased: '',
        preShow: '',
        haspromotionTag: ''
      },
      checkAdd: true,
      checkEdit: true,
      checkDelete: true,
      citylist: [],
      filmlist: [],
      film: {
        cityId: null
      },
      options: [],
      optionsfil: []
    }
  },
  computed: {
    ...mapGetters(['roles']),
    header: {
      set: function(v) {
        Object.keys(this.columns).forEach(key => {
          this.columns[key].show = false
          v.forEach(i => {
            if (key === i) {
              this.columns[key].show = true
            }
          })
        })
        const checkedCount = v.length
        this.checkAll = checkedCount === Object.keys(this.columns).length
        this.isIndeterminate =
          checkedCount > 0 && checkedCount < Object.keys(this.columns).length
        return Object.keys(this.columns).filter(i => this.columns[i].show)
      },
      get: function() {
        var array = Object.keys(this.columns).filter(i => this.columns[i].show)
        return array
      }
    }
  },
  mounted() {
    this.columns = columns()
    var checkedCount = this.header.length
    this.checkAll = checkedCount === Object.keys(this.columns).length
    this.isIndeterminate =
      checkedCount > 0 && checkedCount < Object.keys(this.columns).length
    this.fetchData()
    this.xuanrancity()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      page(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearchClick() {
      if (!this.listQuery.title) {
        this.$message({
          message: '搜索内容不能为空',
          type: 'error',
          duration: 800
        })
      }
    },
    handleSearchCleanClick() {
      this.listQuery.title = ''
    },
    handleSearchShowChange() {
      this.searchShow = !this.searchShow
    },
    handleRefreshClick() {
      this.$message({
        message: '刷新成功',
        type: 'success',
        duration: 800
      })
      this.fetchData()
    },
    handleHeaderClick() {
      this.headerShow = true
    },
    // <<<< 工具栏相关
    handleCheckAllChange(val) {
      this.header = val ? Object.keys(this.columns) : []
      Object.keys(this.columns).forEach(key => {
        this.columns[key].show = val
      })
      this.isIndeterminate = false
    },
    handleSortChange(val) {
      this.listQuery.asc = val.order !== 'descending'
      this.listQuery.order = toLowerCase(val.prop)
      this.fetchData()
    },
    // <<<< 分页相关
    handleSizeChange(val) {
      this.listQuery.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },
    // >>>> 分页相关
    handleEditClick(row) {
      this.findFilm(row.cityId)
      this.isCreate = false
      this.formShow = true
      this.row = Object.assign(row, {})
    },
    handleBeforeClose() {
      this.row = {}
      this.formShow = false
      this.$refs.form.resetFields()
    },
    handleAddClick() {
      this.optionsfil = []
      this.isCreate = true
      this.formShow = true
      this.row = {
        movieName: '',
        star: '',
        sc: '',
        wish: '',
        showst: '',
        showInfo: '',
        price: '',
        cityId: '',
        filmId: '',
        version: '',
        globalReleased: '',
        preShow: '',
        haspromotionTag: ''
      }
    },
    handleUpdateClick() {
      this.$refs.form.validate(valid => {
        if (valid) {
          update(this.row).then(res => {
            this.fetchData()
            this.$message({
              message: res.data,
              type: 'success',
              duration: 800
            })
            this.fileList = []
            this.formShow = false
            this.form = {}
          })
        }
      })
    },
    handleCreateClick() {
      this.$refs.form.validate(valid => {
        if (valid) {
          add(this.row).then(res => {
            this.$message({
              message: res.data,
              type: 'success',
              duration: 800
            })
            this.fileList = []
            this.fetchData()
            this.formShow = false
            this.form = {}
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleDeleteClick(row) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del(row.id).then(res => {
            this.fetchData()
          })
        })
        .catch(() => {
          console.log('取消删除')
        })
    },
    // 上传图片接口
    submitUpload() {
      this.$refs.upload.submit()
    },
    uploadFilesSucceed(res) {
      this.row.haspromotionTag = res.message
      this.fileList = []
    },
    handleRemove(file, fileList) {
    },
    handlePreview(file) {
    },
    xuanrancity() {
      getcitylist().then(res => {
        this.options = res.data.map(item => {
          return {
            label: item.nm,
            value: item.id
          }
        })
      })
    },
    changeSeleCity(value) {
      getfilmlist({ cityId: Number(value) }).then(res => {
        this.filmlist = res.data
        if (res.data.length > 0) {
          this.optionsfil = res.data.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
          this.row.filmId = res.data[0].id
        } else {
          this.row.filmId = ''
          this.optionsfil = []
        }
      })
    },
    findFilm(value) {
      this.film.cityId = parseInt(value)
      getfilmlist(this.film).then(res => {
        this.filmlist = res.data
        if (this.filmlist.length > 0) {
          this.optionsfil = this.filmlist.map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
        }
      })
    }
  }
}
</script>

<style>
</style>
